package Hash;

import java.util.*;


class person{
    public int val;

    public person(int val) {
        this.val = val;
    }
}

public class test {

    public static void main(String[] args) {
        String[]words={"i","love","leetcode","i","love","coding"};
        List<String>list=new ArrayList<>();
list= topKFrequent(words,1);
        System.out.println(list.toString());


    }






    public static void main12(String[] args) {
        HashMap<Integer,Integer>map=new HashMap<>();

    }


    public static List<String> topKFrequent(String[] words, int k) {
HashMap<String,Integer>map=new HashMap<>();
        for (int i = 0; i <words.length ; i++) {
            if(map.containsKey(words[i])){
                int v=map.get(words[i]);
                map.put(words[i],v+1);
            }else {
                map.put(words[i],1);
            }
        }

        PriorityQueue<Map.Entry<String,Integer>>minHeap=new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
         if(o1.getValue().compareTo(o2.getValue())==0){
    return o2.getKey().compareTo(o1.getKey());
         }else {
             return o1.getValue().compareTo(o2.getValue());
         }
            }
        });

        for (Map.Entry<String,Integer> word:map.entrySet()) {
            if (minHeap.size()<k){
                minHeap.offer(word);
            }else {
                Map.Entry<String,Integer>top=minHeap.peek();
                if (word.getValue().compareTo(top.getValue())>0){
                    minHeap.poll();
                    minHeap.offer(word);
                }else if(word.getValue().compareTo(top.getValue())==0){
                    if(word.getKey().compareTo(top.getKey())<0){
                        minHeap.poll();
                        minHeap.offer(word);
                    }
                }
            }
        }

        List<String>list=new ArrayList<>();
        for (int i = 0; i <k ; i++) {
            Map.Entry<String,Integer>map1=minHeap.poll();
            list.add(map1.getKey());
        }
Collections.reverse(list);
return list;
    }


    public static void main21(String[] args) {
        HashBuck2<String ,Integer>ha=new HashBuck2<>();
        ha.put("abc",123);
        ha.put("a",1);
        ha.put("b",2);
        ha.put("ab",12);
        ha.put("ac",13);

        System.out.println(ha.getValues("ab"));


    }



    public static void main23(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        str1.toUpperCase();
        str2.toUpperCase();
        HashSet<Character> set1 = new HashSet<>();
        HashSet<Character> set2 = new HashSet<>();
        for (int i = 0; i < str2.length(); i++) {
            set1.add(str2.charAt(i));
        }
        for (int i = 0; i < str1.length(); i++) {
            if (!set1.contains(str1.charAt(i))) {
                set2.add(str1.charAt(i));
            }
        }
        for (char ch : set2) {

        }

    }


    public static void main2(String[] args) {

        HashMap<Integer,Integer>map=new HashMap<>();
        HashSet<Integer>set=new HashSet<>();



    }


    public static void main1(String[] args) {
        HashBuck hashBuck=new HashBuck();
        hashBuck.put(12,4);
        hashBuck.put(1,4);
        hashBuck.put(2,2);
        hashBuck.put(24,6);
        hashBuck.put(2,5);
        hashBuck.put(135,2);
        hashBuck.put(123,9);
        hashBuck.put(53,40);
        hashBuck.put(182,48);
        hashBuck.put(882,48);
        hashBuck.put(17,434);
        hashBuck.put(1242,45);

        System.out.println(hashBuck.getvalues(17));

    }
}
